//
//  FindController.m
//  traveling
//
//  Created by xiaoR on 16/3/27.
//  Copyright © 2016年 family. All rights reserved.
//

#import "FindController.h"
#import "AFNetworking.h"
#import "JSONModel.h"
#import "DataModel.h"
#import "HPTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "JHRefresh.h"
#import "HPWebViewController.h"
#define baseUrl @"http://open.qyer.com/qyer/special/topic/special_list?"

@interface FindController ()<UITableViewDataSource ,UITableViewDelegate>{
    NSMutableArray *arr;
    UIView *_midMask;
}

@end

@implementation FindController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createNavigationBar:9];
    self.back.text = @"发现";
    //调用初始化数据
    [self initData];
    //调用创建视图
    [self createView];
}

-(void)initData{
    _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
    self.pageNum = 1;
    [self getNetData];
    arr = [[NSMutableArray alloc]initWithCapacity:0];
    
}
-(void)getNetData{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setHTTPShouldHandleCookies:YES];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    __weak typeof(self) weakSelf = self;
    [manager GET:baseUrl parameters:@{@"client_id":@"qyer_ios",@"client_secret":@"cd254439208ab658ddf9",@"count":@"20",@"page":[NSString stringWithFormat:@"%ld",(long)_pageNum]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        if (weakSelf.pageNum==1) {
            [weakSelf.dataArray removeAllObjects];
        }
        arr = [ dict objectForKey:@"data"];
        [weakSelf.dataArray addObjectsFromArray:arr];
        if (weakSelf.dataSource) {
            weakSelf.dataSource = nil;
        }
        weakSelf.dataSource = [[JSONModelArray alloc]initWithArray:weakSelf.dataArray modelClass:[DataModel class]];
        [weakSelf.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
-(void)createView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-110) style:UITableViewStylePlain];
    _tableView.dataSource =self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [self addRefresh];
}
-(void)addRefresh{
    __weak typeof (self) weakSelf =self;
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshAmazingAniView class] beginRefresh:^{
        weakSelf.pageNum = 1;
        [weakSelf getNetData];
        [weakSelf.tableView headerEndRefreshingWithResult:JHRefreshResultSuccess];
    }];
    [self.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshAmazingAniView class] beginRefresh:^{
        weakSelf.pageNum  ++;
        [weakSelf getNetData];
        [weakSelf.tableView footerEndRefreshing];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark - UITableViewDataSource&UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DataModel *model = [_dataSource objectAtIndex:indexPath.row];
    static NSString *identifier = @"HPCell";
    HPTableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identifier ];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"HPTableViewCell" owner:self options:nil]lastObject];
    }
    
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.photo]];				
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DataModel *dataModel = [_dataSource objectAtIndex:indexPath.row];
    HPWebViewController *hwc = [[HPWebViewController alloc]init];
    [hwc initDataUrl:dataModel.url];
    [hwc setHidesBottomBarWhenPushed:YES];
  [self.navigationController pushViewController:hwc animated:YES];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
